package com.wzy.netty.core.dubborpc.netty;

import com.wzy.netty.core.dubborpc.provider.HelloServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 描述  :
 * 创建人:    Wzy
 * 创建时间: 2020-06-12  21:46
 */
public class NettyServerHandel extends ChannelInboundHandlerAdapter {
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("msg=" + msg);
        //客户端 调用服务api,需要定义一个协议
        /* 要求: 每次发消息时,必须以某个字符串开头 "HelloService#hello#****"*/
        if (msg.toString().startsWith("HelloService#hello#")) {
            String result = new HelloServiceImpl().hello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1));
            ctx.writeAndFlush(result);
        }
    }
}
